package cn.zn.framework.common.utils;

import cn.zn.framework.common.exception.RRException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.Map;

public class UniqueVerUtils<T> {

    public void uniqueVer(Object id, Map<String, Object> verMap, IService<T> service) {
        this.uniqueVer(id, verMap, service, "已存在相同记录，请重新填写！");
    }

    public void uniqueVer(Object id, Map<String, Object> verMap, IService<T> service, String msg) {
        // 判断是否重复
        QueryWrapper<T> queryWrapper = Wrappers.query();
        verMap.forEach(queryWrapper::eq);
        if (id != null) {
            queryWrapper.ne("id", id);
        }
        if (service.count(queryWrapper) > 0) {
            throw new RRException(msg);
        }
    }
}
